//
//  HMPopMenu.m
//  黑马微博-wzy
//
//  Created by Mac on 2016/12/4.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "HMPopMenu.h"

@interface HMPopMenu()

@property(nonatomic, strong) UIView *contentView;

//最底部的遮盖,屏蔽除菜单以外控件的事件
@property (nonatomic, weak) UIButton *coverButton;

//容器,容纳具体要显示的内容
@property (nonatomic, weak) UIImageView *containerImageView;


@end



@implementation HMPopMenu

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //添加菜单内的2个子控件
        //添加一个遮盖按钮
        UIButton *coverBtn = [[UIButton alloc] init];
        coverBtn.backgroundColor = [UIColor clearColor];
        [coverBtn addTarget:self action:@selector(coverBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:coverBtn];
        self.coverButton = coverBtn;
        
        //添加带箭头的菜单图片
        UIImageView *container = [[UIImageView alloc] init];
        container.userInteractionEnabled = YES;
        [self addSubview:container];
        self.containerImageView = container;
        
        //默认箭头指向中间
        self.arrowPosition = HMPopMenuArrowPositionCrnter;
        
    }
    return self;
}

- (instancetype)initWithContentView:(UIView *)contentView{

    if (self = [super init]) {
        self.contentView = contentView;
    }
    return self;
}


+ (instancetype)popMenuWithContentView:(UIView *)contentView{

    return [[self alloc] initWithContentView:contentView];
}

- (void)layoutSubviews{

    [super layoutSubviews];
    
//    NSLog(@"8888888");
    self.coverButton.frame = self.bounds;
}

#pragma mark ======= 内部 方法 ========
- (void)coverBtnClick{

    [self dismiss];
}



#pragma mark -------- 公共方法 ---------
- (void)setDimBackground:(BOOL)dimBackground{
    _dimBackground = dimBackground;
    if (dimBackground) {
        self.coverButton.backgroundColor = [UIColor blackColor];
        self.coverButton.alpha = 0.3;
    }else{
        self.coverButton.backgroundColor = [UIColor clearColor];
        self.coverButton.alpha = 1.0;
    }

}

- (void)setArrowPosition:(HMPopMenuArrowPosition)arrowPosition{
    _arrowPosition = arrowPosition;
    switch (arrowPosition) {
        case HMPopMenuArrowPositionCrnter:
            self.containerImageView.image = [UIImage resizedImage:@"popover_background"];
            break;
        case HMPopMenuArrowPositionLeft:
            self.containerImageView.image = [UIImage resizedImage:@"popover_background_left"];
            break;
        case HMPopMenuArrowPositionRight:
            self.containerImageView.image = [UIImage resizedImage:@"popover_background_right"];
            break;
            
        default:
            break;
    }

}


//设置菜单背景图片
- (void)setBackground:(UIImage *)background{
    self.containerImageView.image = background;
}

//显示菜单
- (void)showInRect:(CGRect)rect{
//添加菜单整体到窗口上
    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    self.frame = window.bounds;
    [window addSubview:self];
    
    //设置容器的frame
    self.containerImageView.frame = rect;
    [self addSubview:self.containerImageView];
    
    //设置容器里面内容的frame
    CGFloat topMargin = 12;
    CGFloat leftMargin = 5;
    CGFloat rightMargin = 5;
    CGFloat bottomMargin = 8;
    
    self.containerImageView.y = topMargin;
    self.containerImageView.x = leftMargin;
    self.containerImageView.width = self.containerImageView.width - leftMargin - rightMargin;
    self.containerImageView.height = self.containerImageView.height - topMargin - bottomMargin;
}

//关闭菜单
- (void)dismiss{
    if ([self.deletegate respondsToSelector:@selector(popMenuDidDismissed:)]) {
        [self.deletegate popMenuDidDismissed:self];
    }
    [self removeFromSuperview];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
